package com.aco.party.service.system;

import com.aco.party.dao.custom.HireDao;
import com.aco.party.dao.custom.UserDao;
import com.aco.party.enums.VisitTypeEnum;
import com.aco.party.model.po.Hire;
import com.aco.party.model.po.User;
import com.aco.party.model.vo.HireParam;
import com.aco.party.model.vo.HireVo;
import com.aco.party.model.vo.VisitParam;
import com.aco.party.model.vo.common.PageParam;
import com.aco.party.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by flying on 3/10/2016 0010.
 */
@Service
public class HireService {
    @Resource
    private HireDao hireDao;

    @Resource
    private VisitService visitService;

    @Resource
    private UserDao userDao;

    public List<HireVo> getByPage(PageParam pageParam,HireParam param) {
        List<HireVo> list = new ArrayList();
        List<Hire> hires = hireDao.getList(pageParam,param);
        HireVo vo;
        if (hires != null) {
            for (Hire hire : hires) {
                vo = new HireVo();
                BeanUtils.copyProperties(hire, vo);
                VisitParam visitParam = new VisitParam();
                visitParam.setType(VisitTypeEnum.招聘.getValue());
                visitParam.setTargetId(vo.getId());
                vo.setViews(visitService.getCountByParam(visitParam).longValue());
                if(vo.getCreator() != null && vo.getCreator() != 0){
                    User user = userDao.selectByPrimaryKey(vo.getCreator());
                    if(user != null) {
                        vo.setCreatorStr(user.getNickName());
                    }
                }
                list.add(vo);
            }
        }
        return list;
    }

    public Integer getTotal(HireParam param) {
        return hireDao.getTotal(param);
    }

    public void update(Hire hire) {
        hire.setUpdateTime(new Date());
        hireDao.updateByPrimaryKeySelective(hire);
    }

    public void add(Hire hire) {
        hire.setCreateTime(new Date());
        hireDao.insertSelective(hire);
    }

    public void delete(Long id) {
        hireDao.deleteByPrimaryKey(id);
    }

    public Hire getByPrimaryKey(Long id) {
        return hireDao.selectByPrimaryKey(id);
    }

    public HireVo getVoById(Long id) {
        HireVo vo = new HireVo();
        Hire hire = hireDao.selectByPrimaryKey(id);
        if(hire != null) {
            BeanUtils.copyProperties(hire, vo);
        }
        return vo;
    }

    public int getThisWeekNewWork(){
        Date now = new Date();
        Date lastWeek = DateUtil.addDay(now,-6);
        String begin = DateUtil.dateToString(lastWeek, "yyyy-MM-dd") + "00:00:00";
        String end = DateUtil.dateToString(now,"yyyy-MM-dd") + "23:59:59";
        return hireDao.getThisWeekNewWork(begin,end);
    }
}
